import Mock from "mockjs";
//
import {enabled,timeout,defaultData,ApiModule} from "../config/mock.config"
//
if(enabled){
  // 设置拦截ajax请求的相应时间
  Mock.setup({ timeout });
  // 查找所有 data 下的 ts 文件
  const datas = require.context('./data',true,/\.ts$/)
  //
  datas.keys().forEach(key => {
    const d:any[] = datas(key).default;
    if (!d) return;
    d.forEach(item => {
      const disabled = item.disabled;
      if (disabled) {
        return;
      }
      //
      const url = item.url;
      const method = item.method || "";
      const mock = item.mock || {};
      const tm = typeof mock;
      const res = item.result || {};
      //
      Mock.mock(url, method, (option:any) : ApiModule => {
        const result =
          tm === "function" ? mock.call(this, option, Mock) : Mock.mock(mock);
        return { ...defaultData,...res, result };
      });
    })
  })
} 


